@posthog/ai 8.20.1 → 8.21.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/README.md +20 -0
- package/dist/adk/index.cjs +125 -36
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.d.ts +7 -0
- package/dist/adk/index.mjs +126 -37
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +115 -22
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +7 -0
- package/dist/anthropic/index.mjs +116 -23
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +131 -35
- package/dist/claude-agent-sdk/index.cjs.map +1 -1
- package/dist/claude-agent-sdk/index.d.ts +6 -0
- package/dist/claude-agent-sdk/index.mjs +132 -36
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +116 -23
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +7 -0
- package/dist/gemini/index.mjs +116 -23
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +138 -43
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.d.ts +7 -0
- package/dist/genkit/index.mjs +138 -43
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +114 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.mjs +115 -22
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +99 -21
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +7 -0
- package/dist/langchain/index.mjs +99 -21
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +99 -21
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.d.ts +6 -0
- package/dist/langchain/middleware/index.mjs +99 -21
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +115 -22
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +7 -0
- package/dist/openai/index.mjs +116 -23
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +139 -60
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.d.ts +7 -0
- package/dist/openai-agents/index.mjs +139 -60
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs +24 -14
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs +24 -14
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs +24 -14
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +3 -3
- package/dist/otel/index.mjs +24 -14
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +114 -21
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.d.ts +6 -0
- package/dist/vercel/index.mjs +115 -22
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/genkit/index.cjs
CHANGED
|
@@ -1,17 +1,85 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let uuid = require("uuid");
|
|
3
2
|
let _posthog_core = require("@posthog/core");
|
|
3
|
+
let uuid = require("uuid");
|
|
4
4
|
//#region package.json
|
|
5
|
-
var version = "8.
|
|
5
|
+
var version = "8.21.0";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/captureAiEvent.ts
|
|
8
8
|
/** @internal */
|
|
9
9
|
function isFullAiCaptureEnabled(client) {
|
|
10
10
|
return client?.enableFullAiCapture === true;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
function userDistinctId(value) {
|
|
18
|
+
if (!value) return;
|
|
19
|
+
const id = String(value);
|
|
20
|
+
return id.trim() === "" ? void 0 : id;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The caller's per-event options when they are an object. A missing or `null` value means no
|
|
24
|
+
* options. An array or any other value is reported and ignored, so it is never spread.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
function readAiEventOptions(value) {
|
|
29
|
+
if (value === void 0 || value === null) return;
|
|
30
|
+
if (typeof value === "object" && !Array.isArray(value)) return value;
|
|
31
|
+
console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
|
|
35
|
+
* processing off and beats the caller's `process_person_profile`.
|
|
36
|
+
*
|
|
37
|
+
* posthog-node 6 takes the options as they are, so no option is written as a property.
|
|
38
|
+
* posthog-node 5 has no per-event options: each known option is sent as the property that version
|
|
39
|
+
* reads, and an unknown key is dropped.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
function eventOptionFields(client, properties, options, personless) {
|
|
44
|
+
const userOptions = readAiEventOptions(options);
|
|
45
|
+
if (isPostHogNode6(client)) {
|
|
46
|
+
const eventOptions = {
|
|
47
|
+
...userOptions,
|
|
48
|
+
...personless ? { process_person_profile: false } : {}
|
|
49
|
+
};
|
|
50
|
+
return Object.keys(eventOptions).length > 0 ? {
|
|
51
|
+
properties,
|
|
52
|
+
options: eventOptions
|
|
53
|
+
} : { properties };
|
|
54
|
+
}
|
|
55
|
+
const withOptionProperties = {
|
|
56
|
+
...properties,
|
|
57
|
+
...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
|
|
58
|
+
};
|
|
59
|
+
if (personless) withOptionProperties.$process_person_profile = false;
|
|
60
|
+
return { properties: withOptionProperties };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether the client is posthog-node 6 or later, the first major with per-event options.
|
|
64
|
+
*
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
function isPostHogNode6(client) {
|
|
68
|
+
return (0, _posthog_core.acceptsEventOptions)(client);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
|
|
72
|
+
* controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
|
|
73
|
+
* upgrade never changes how a 5.x client sends.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
function usesAiLane(client) {
|
|
78
|
+
return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
|
|
79
|
+
}
|
|
12
80
|
/** @internal */
|
|
13
81
|
function captureAiEvent(client, event) {
|
|
14
|
-
if (
|
|
82
|
+
if (usesAiLane(client) && typeof client.captureAi === "function") {
|
|
15
83
|
client.captureAi(event);
|
|
16
84
|
return;
|
|
17
85
|
}
|
|
@@ -19,7 +87,7 @@ function captureAiEvent(client, event) {
|
|
|
19
87
|
}
|
|
20
88
|
/** @internal */
|
|
21
89
|
async function captureAiEventImmediate(client, event) {
|
|
22
|
-
if (
|
|
90
|
+
if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
|
|
23
91
|
await client.captureAiImmediate(event);
|
|
24
92
|
return;
|
|
25
93
|
}
|
|
@@ -172,13 +240,17 @@ var MediaTypeContext = class MediaTypeContext {
|
|
|
172
240
|
//#region src/sanitization/binary_content_redactor.ts
|
|
173
241
|
const STRONG_CONTEXT_MIN_LENGTH = 64;
|
|
174
242
|
const WEAK_CONTEXT_MIN_LENGTH = 1024;
|
|
243
|
+
const MAX_NODES$1 = 1e4;
|
|
244
|
+
const TRUNCATED$1 = "[Truncated]";
|
|
175
245
|
var BinaryContentRedactor = class {
|
|
176
246
|
constructor(recognizer = new Base64Recognizer()) {
|
|
177
247
|
this.recognizer = recognizer;
|
|
178
|
-
this.
|
|
248
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
249
|
+
this.remainingNodes = MAX_NODES$1;
|
|
179
250
|
}
|
|
180
251
|
redact(value, mediaType) {
|
|
181
|
-
this.
|
|
252
|
+
this.ancestors = /* @__PURE__ */ new WeakSet();
|
|
253
|
+
this.remainingNodes = MAX_NODES$1;
|
|
182
254
|
return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
|
|
183
255
|
}
|
|
184
256
|
/** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
|
|
@@ -190,18 +262,24 @@ var BinaryContentRedactor = class {
|
|
|
190
262
|
if (typeof value === "string") return this.redactLeaf(value, ctx);
|
|
191
263
|
if (typeof value !== "object") return value;
|
|
192
264
|
if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
|
|
193
|
-
if (this.
|
|
194
|
-
this.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
265
|
+
if (this.ancestors.has(value)) return null;
|
|
266
|
+
if (this.remainingNodes <= 0) return TRUNCATED$1;
|
|
267
|
+
this.remainingNodes--;
|
|
268
|
+
this.ancestors.add(value);
|
|
269
|
+
try {
|
|
270
|
+
if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
|
|
271
|
+
const obj = value;
|
|
272
|
+
const out = {};
|
|
273
|
+
for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
|
|
274
|
+
value: this.walk(obj[k], new MediaTypeContext(obj, k)),
|
|
275
|
+
enumerable: true,
|
|
276
|
+
writable: true,
|
|
277
|
+
configurable: true
|
|
278
|
+
});
|
|
279
|
+
return out;
|
|
280
|
+
} finally {
|
|
281
|
+
this.ancestors.delete(value);
|
|
282
|
+
}
|
|
205
283
|
}
|
|
206
284
|
redactString(value, ctx) {
|
|
207
285
|
const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
|
|
@@ -511,6 +589,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
|
|
|
511
589
|
//#endregion
|
|
512
590
|
//#region src/captureAiGeneration.ts
|
|
513
591
|
/**
|
|
592
|
+
* posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
|
|
593
|
+
* arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
|
|
594
|
+
*/
|
|
595
|
+
const captureAiException = (client, error, properties, uuid$1) => {
|
|
596
|
+
if (isPostHogNode6(client)) {
|
|
597
|
+
client.captureException(error, {
|
|
598
|
+
properties,
|
|
599
|
+
uuid: uuid$1
|
|
600
|
+
});
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
client.captureException.call(client, error, void 0, properties, uuid$1);
|
|
604
|
+
};
|
|
605
|
+
/**
|
|
514
606
|
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
515
607
|
*
|
|
516
608
|
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
@@ -546,7 +638,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
546
638
|
if (client.options?.enableExceptionAutocapture) {
|
|
547
639
|
if (!shouldRedact) {
|
|
548
640
|
exceptionId = (0, _posthog_core.uuidv7)();
|
|
549
|
-
client
|
|
641
|
+
captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
|
|
550
642
|
}
|
|
551
643
|
if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
|
|
552
644
|
}
|
|
@@ -572,6 +664,7 @@ const captureAiGeneration = async (client, options) => {
|
|
|
572
664
|
...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
|
|
573
665
|
...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
|
|
574
666
|
};
|
|
667
|
+
const distinctId = userDistinctId(options.distinctId);
|
|
575
668
|
const properties = {
|
|
576
669
|
$ai_lib: "posthog-ai",
|
|
577
670
|
$ai_lib_version: version,
|
|
@@ -591,7 +684,6 @@ const captureAiGeneration = async (client, options) => {
|
|
|
591
684
|
...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
|
|
592
685
|
...options.properties,
|
|
593
686
|
$ai_tokens_source: getTokensSource(options.properties),
|
|
594
|
-
...options.distinctId ? {} : { $process_person_profile: false },
|
|
595
687
|
...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
|
|
596
688
|
...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
|
|
597
689
|
...options.completionId ? { $ai_completion_id: options.completionId } : {},
|
|
@@ -600,9 +692,9 @@ const captureAiGeneration = async (client, options) => {
|
|
|
600
692
|
...costOverrideData
|
|
601
693
|
};
|
|
602
694
|
const event = {
|
|
603
|
-
distinctId:
|
|
695
|
+
distinctId: distinctId ?? traceId,
|
|
604
696
|
event: eventType,
|
|
605
|
-
properties,
|
|
697
|
+
...eventOptionFields(client, properties, options.options, !distinctId),
|
|
606
698
|
groups: options.groups
|
|
607
699
|
};
|
|
608
700
|
if (options.captureImmediate) await captureAiEventImmediate(client, event);
|
|
@@ -749,15 +841,16 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
749
841
|
constructor(options) {
|
|
750
842
|
this._spanDistinctIds = /* @__PURE__ */ new WeakMap();
|
|
751
843
|
this._client = options.client;
|
|
752
|
-
this._distinctId = options.distinctId;
|
|
844
|
+
this._distinctId = userDistinctId(options.distinctId);
|
|
753
845
|
this._privacyMode = options.privacyMode ?? false;
|
|
754
846
|
this._groups = options.groups;
|
|
755
847
|
this._properties = options.properties ?? {};
|
|
848
|
+
this._eventOptions = options.options;
|
|
756
849
|
this._onError = options.onError;
|
|
757
850
|
}
|
|
758
851
|
onStart(span) {
|
|
759
852
|
try {
|
|
760
|
-
const distinctId = this._distinctId ?? this._client.getContext?.()?.distinctId;
|
|
853
|
+
const distinctId = this._distinctId ?? userDistinctId(this._client.getContext?.()?.distinctId);
|
|
761
854
|
if (distinctId !== void 0) this._spanDistinctIds.set(span, distinctId);
|
|
762
855
|
} catch (error) {
|
|
763
856
|
this._handleError(error);
|
|
@@ -813,6 +906,7 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
813
906
|
},
|
|
814
907
|
stopReason: finishReason,
|
|
815
908
|
groups: this._groups,
|
|
909
|
+
options: this._eventOptions,
|
|
816
910
|
privacyMode: this._privacyMode,
|
|
817
911
|
onError: this._onError,
|
|
818
912
|
error: spanError(span),
|
|
@@ -840,6 +934,7 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
840
934
|
output: null,
|
|
841
935
|
latency: latencySeconds(span),
|
|
842
936
|
groups: this._groups,
|
|
937
|
+
options: this._eventOptions,
|
|
843
938
|
privacyMode: this._privacyMode,
|
|
844
939
|
onError: this._onError,
|
|
845
940
|
error: spanError(span),
|
|
@@ -856,28 +951,28 @@ var PostHogGenkitSpanProcessor = class {
|
|
|
856
951
|
const isRoot = span.attributes["genkit:isRoot"] === true;
|
|
857
952
|
const { traceId, spanId, parentId } = spanIds(span);
|
|
858
953
|
const error = spanError(span);
|
|
954
|
+
const properties = {
|
|
955
|
+
$ai_lib: "posthog-ai",
|
|
956
|
+
$ai_lib_version: version,
|
|
957
|
+
$ai_framework: "genkit",
|
|
958
|
+
$ai_trace_id: traceId,
|
|
959
|
+
$ai_span_id: spanId,
|
|
960
|
+
...!isRoot && parentId ? { $ai_parent_id: parentId } : {},
|
|
961
|
+
$ai_span_name: span.name,
|
|
962
|
+
$ai_span_type: span.attributes["genkit:metadata:subtype"] ?? span.attributes["genkit:type"],
|
|
963
|
+
$ai_input_state: this._formatState(span, "genkit:input"),
|
|
964
|
+
$ai_output_state: this._formatState(span, "genkit:output"),
|
|
965
|
+
$ai_latency: latencySeconds(span),
|
|
966
|
+
...error ? {
|
|
967
|
+
$ai_is_error: true,
|
|
968
|
+
$ai_error: formatAiError(error, isPrivacyModeEnabled(this._client, this._privacyMode))
|
|
969
|
+
} : {},
|
|
970
|
+
...this._properties
|
|
971
|
+
};
|
|
859
972
|
captureAiEvent(this._client, {
|
|
860
973
|
distinctId: distinctId ?? traceId,
|
|
861
974
|
event: isRoot ? "$ai_trace" : "$ai_span",
|
|
862
|
-
properties
|
|
863
|
-
$ai_lib: "posthog-ai",
|
|
864
|
-
$ai_lib_version: version,
|
|
865
|
-
$ai_framework: "genkit",
|
|
866
|
-
$ai_trace_id: traceId,
|
|
867
|
-
$ai_span_id: spanId,
|
|
868
|
-
...!isRoot && parentId ? { $ai_parent_id: parentId } : {},
|
|
869
|
-
$ai_span_name: span.name,
|
|
870
|
-
$ai_span_type: span.attributes["genkit:metadata:subtype"] ?? span.attributes["genkit:type"],
|
|
871
|
-
$ai_input_state: this._formatState(span, "genkit:input"),
|
|
872
|
-
$ai_output_state: this._formatState(span, "genkit:output"),
|
|
873
|
-
$ai_latency: latencySeconds(span),
|
|
874
|
-
...error ? {
|
|
875
|
-
$ai_is_error: true,
|
|
876
|
-
$ai_error: formatAiError(error, isPrivacyModeEnabled(this._client, this._privacyMode))
|
|
877
|
-
} : {},
|
|
878
|
-
...this._properties,
|
|
879
|
-
...distinctId ? {} : { $process_person_profile: false }
|
|
880
|
-
},
|
|
975
|
+
...eventOptionFields(this._client, properties, this._eventOptions, !distinctId),
|
|
881
976
|
groups: this._groups
|
|
882
977
|
});
|
|
883
978
|
}
|