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